package com.xian.wsim.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserOnlineService {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    private static final String ONLINE_USERS_KEY = "online_users";

    public void setUserOnline(Long userId) {
        redisTemplate.opsForSet().add(ONLINE_USERS_KEY, userId.toString());
    }

    public void setUserOffline(Long userId) {
        redisTemplate.opsForSet().remove(ONLINE_USERS_KEY, userId.toString());
    }

    public boolean isUserOnline(Long userId) {
        return redisTemplate.opsForSet().isMember(ONLINE_USERS_KEY, userId.toString());
    }
}
